//
//  GNewFeatureViewController.swift
//  sinaWeiBoAI
//
//  Created by Z.J on 16/3/22.
//  Copyright © 2016年 Z.J. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class GNewFeatureViewController: UICollectionViewController {

    let layer = UICollectionViewFlowLayout()
    // 父类没有init()这个构造函数,子类不需要使用overridr
    init()
    {
        super.init(collectionViewLayout:layer)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        prepareCollectionView()
    
        self.collectionView!.registerClass(GNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }
    
    /// collectionView的设置
    private func prepareCollectionView()
    {
        layer.itemSize = UIScreen.mainScreen().bounds.size
        layer.minimumInteritemSpacing = 0
        layer.minimumLineSpacing = 0
        layer.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        
    }

}

// MARK: - 数据源及代理方法
extension GNewFeatureViewController
{
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return GNewFeatuerPageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! GNewFeatureCell
        
//        cell.backgroundColor = UIColor.random()
        // 设置显示的页码
        cell.index = indexPath.item
        
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        // 拿到偏移值
        let offsetX = scrollView.contentOffset.x
        // 查看当前多少页
        let page = Int(offsetX / UIScreen.mainScreen().bounds.width)
        // 判断当前是否是最后一页
        if page == GNewFeatuerPageCount - 1
        {
        // 取出cell
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! GNewFeatureCell
        
        cell.startButtonAnimation()
        }
    }
}


// MARK: - 自定义cell
class GNewFeatureCell:UICollectionViewCell
{
    /// 当前显示的页数
    var index:Int = 0
    {
        didSet{
            // 当值发生改变
            self.bkgImageView.image = UIImage(named: "new_feature_\(index + 1)")
            // 只要设置图片就隐藏按钮
            self.startBtn.hidden = true
        }
    }
    
   
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /// cell动画
    func startButtonAnimation()
    {
        startBtn.hidden = false
        // 在开始动画之前,将transform Scale设置为0
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 相当于 回复默认状态
            self.startBtn.transform = CGAffineTransformIdentity
            
            }, completion: nil)
    }
    
    
    /// 添加UI
    private func prepareUI()
    {
        // 添加到cell
        contentView.addSubview(bkgImageView)
        contentView.addSubview(startBtn)
        
        // 2.添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        startBtn.translatesAutoresizingMaskIntoConstraints = false
        // 背景
        contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        // 开始按钮
        contentView.addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
    }
    
    
    // MARK: - 懒加载
    
    /// 背景图片
    private lazy var bkgImageView : UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    /// 开始按钮
    private lazy var startBtn : UIButton =
    {
        let btn = UIButton(type: UIButtonType.Custom)
        
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)

        return btn
        
        
    }()
    
    
    
}





